<template>
  <div class="mod-config" >
    <el-dialog
      v-el-drag-dialog
      title="秒杀活动"
      :visible.sync="dialogVisible"
      width="80%">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.keywords" placeholder="活动标题" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;height: 500px;overflow-y: inherit;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="activityId"
          header-align="center"
          align="center"
          label="活动ID">
        </el-table-column>
        <el-table-column
          prop="activityTitle"
          header-align="center"
          align="center"
          label="活动标题">
        </el-table-column>
        <el-table-column
          prop="beginTime"
          header-align="center"
          align="center"
          label="活动开始时间">
        </el-table-column>
        <el-table-column
          prop="endTime"
          header-align="center"
          align="center"
          label="活动结束时间">
        </el-table-column>
        <el-table-column
          prop="publishRemark"
          header-align="center"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          label="活动状态">
          <template slot-scope="scope">
            <el-switch
              :disabled="true"
              v-model="scope.row.state === 1"
              active-text="线上"
              inactive-text="下线">
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <el-row :gutter="20">
        <el-col :offset="21" style="margin-top: 15px">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="dataListSelections.length === 0"  @click="dataSubmit">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        updateTime: new Date(),
        updateState: false,
        dialogVisible: false,
        updateStateData: {
          id: Number(),
          state: true
        },
        dataForm: {
          keywords: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dialogVisible = true
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/activity/spike/queryActivityList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      dataSubmit () {
        let data = this.dataListSelections.map(data => { return data.id })
        if (data.length !== 1) {
          this.$message.error('只能选择一个活动')
          return
        }
        this.$emit('addSpikeActivity', {data: data[0], showData: '秒杀活动：' + this.dataListSelections[0].activityTitle})
        this.dialogVisible = false
      }
    }
  }
</script>
<style>

  .el-row {
    margin-bottom: 20px;
    &:last-child {
       margin-bottom: 0;
     }
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
